package com.qyhl.webtv.module_microvideo.shortvideo.user.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.commonlib.utils.view.GridDividerItemDecoration;
import com.qyhl.webtv.module_microvideo.R;
import com.qyhl.webtv.module_microvideo.R2;
import com.qyhl.webtv.module_microvideo.shortvideo.user.ShortVideoUserActivity;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ShortVideoUserFragment extends BaseFragment implements ShortVideoUserContract.ShortVideoUserView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private CommonAdapter commonAdapter;

    private ShortVideoUserPresenter mPresenter;

    private List<ShortVideoBean> mDataList = new ArrayList<>();

    private int page = 1;//分页

    private String userPhone;

    private int type;

    private String activityId;

    public void setType(int type) {
        this.type = type;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public void setActivityId(String activityId) {
        this.activityId = activityId;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.micro_fragment_short_video_user, null);
    }

    @Override
    protected void create() {
        mPresenter = new ShortVideoUserPresenter(this);
        initView();
    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getUserWorks(userPhone, page + "", type);
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    public static ShortVideoUserFragment newInstance(String activityId, String userPhone, int type) {
        ShortVideoUserFragment fragment = new ShortVideoUserFragment();
        fragment.setUserPhone(userPhone);
        fragment.setType(type);
        fragment.setActivityId(activityId);
        return fragment;
    }



    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setEnableLoadMore(true);
        refresh.setNestedScrollingEnabled(true);

        recycleView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        recycleView.addItemDecoration(new GridDividerItemDecoration(4, getResources().getColor(R.color.micro_short_video_bg)));

        recycleView.setAdapter(commonAdapter = new CommonAdapter<ShortVideoBean>(getContext(), R.layout.micro_item_short_video_user, mDataList) {
            @Override
            protected void convert(ViewHolder holder, ShortVideoBean bean, int position) {
                holder.setText(R.id.summary, bean.getTitle());
                holder.setText(R.id.hot_value, bean.getHitCount() + "");
                holder.setText(R.id.tagName, bean.getTagName());
                ImageView cover = holder.getView(R.id.cover);
                Glide.with(getContext())
                        .load(bean.getLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_large_default).error(R.drawable.cover_large_default))
                        .into(cover);
            }
        });
    }

    private void setListeners() {
        refresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getUserWorks(userPhone, page + "", type);
        });

        refresh.setOnLoadMoreListener(refreshLayout -> mPresenter.getUserWorks(userPhone, page + "", type));

        loadMask.setOnReloadListener(v -> {
            loadMask.setReloadButtonText("加载中...");
            page = 1;
            mPresenter.getUserWorks(userPhone, page + "", type);
        });

        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                ((ShortVideoUserActivity) getActivity()).showHideBottombar(dy);
            }
        });


        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", (Serializable) mDataList);
                bundle.putString("actId", userPhone);
                bundle.putInt("position", position);
                bundle.putInt("page", page);
                bundle.putInt("type", type);
                RouterManager.router(ARouterPathConstant.SHORT_VIDEO_PLAY,bundle);

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public void setData(List<ShortVideoBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);
        page++;
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            mDataList.clear();
        }
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            loadMask.setErrorText(msg);
            if (msg.contains("网络")) {
                loadMask.setErrorImage(R.drawable.error_network);
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
            }
        }
    }
}
